package com.start.shop.modules.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.start.shop.common.constant.RspCode;
import com.start.shop.common.constant.TableFeildCode;
import com.start.shop.common.exception.MyException;
import com.start.shop.modules.repository.dao.CouponConfigDao;
import com.start.shop.modules.repository.dao.CouponDao;
import com.start.shop.modules.repository.entity.Coupon;
import com.start.shop.modules.repository.entity.CouponConfig;
import com.start.shop.modules.service.ICouponConfigService;
import com.start.shop.modules.service.ICouponService;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class CouponServiceImpl implements ICouponService {
		
	@Autowired
	CouponDao couponDao;
	
	@Autowired
	CouponConfigDao couponConfigDao;
	
	@Autowired
	ICouponConfigService couponConfigService;
	

	/*
	 * 根据状态查询客户的优惠券
	 */
	@Override
	public PageInfo<Coupon> getCouponsByStatus(int userId, int status, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<Coupon> (couponDao.selectCouponsByStatus(userId, status));
	}
	

	/**
	 *优惠券核销
	 */
	@Override
	public CouponConfig writeOffCoupon(int userId, int couponId, BigDecimal orderAmt, int goodsId, int goodsType) {
		
		//查找核销的优惠券
		Coupon coupon = couponDao.selectCouponByCouponId(couponId, userId);
		if(coupon == null) {
			throw new MyException(RspCode.COUPON_NOT_FOUND);
		}
		
		//检查优惠券状态
		if (!coupon.getCouponStatus().equals(TableFeildCode.T_COUPON_STATUS_NOTUSE.getCode())) {
		    log.error("优惠券状态不可用：{}", coupon.getCouponStatus());
			throw new MyException(RspCode.COUPON_CANNOT_USE);
		}
		
		//检查优惠券是否可用
		if(!checkCouponCanUse(coupon.getSortId(), orderAmt, goodsId, goodsType)) {
			throw new MyException(RspCode.COUPON_CANNOT_USE);
		}
		
		//获取优惠券配置信息
		CouponConfig couponConfig = couponConfigService.getCouponConifgById(coupon.getSortId());
		
		//核销优惠券
		updateCouponStatusByCouponId(userId, couponId, TableFeildCode.T_COUPON_STATUS_USED.getCode());
		
		return couponConfig;
	}
	
	/**
	 * 更新优惠券状态
	 */
	@Override
	public void updateCouponStatusByCouponId(int userId, int couponId, String status) {
		couponDao.updateCouponStatusByCouponId(couponId, status);
	}
	
	
	/**
	 *   检查订单是否可用该类型优惠券  0-不可用   1-可用
	 */
	@Override
	public boolean checkCouponCanUse(int sortId, BigDecimal orderAmt, int goodsId, int goodsType) {
		//检查优惠券是否可用
		CouponConfig couponConfig = couponConfigService.getCouponConifgById(sortId);
		//订单金额检查
		if(couponConfig.getCouponCondition().substring(0, 1).equals("1")) {
			if (orderAmt.compareTo(couponConfig.getConditionAmt())<0) {
				log.error("订单金额【{}】不满足条件【{}】", orderAmt, couponConfig.getConditionAmt());
				return false;
			}
			
		}
		//订单指定商品检查
		if(couponConfig.getCouponCondition().substring(1, 2).equals("1")) {
			if (goodsId != couponConfig.getConditionGoods()) {
				log.error("指定商品不满足goodsId【{}】 条件商品【{}】", goodsId, couponConfig.getConditionGoods());
				return false;
			}
		}
		//订单商品类型检查
		if(couponConfig.getCouponCondition().substring(2, 3).equals("1")) {
			if (goodsType != couponConfig.getConditionGoodsType()) {
				log.error("指定商品类型不满足");
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 查找可用的优惠券
	 */
	@Override
	public PageInfo<Coupon> getUseableCoupon(int userId, BigDecimal orderAmt, int goodsId, int goodsType, int pageNum, int pageSize) {
		//创建页面对象
        PageHelper.startPage(pageNum, pageSize);
        
        List<Coupon> couponList = couponDao.selectUseableCoupon(userId, orderAmt, goodsId, goodsType);
		//生成页面信息
        return new PageInfo<Coupon>(couponList);
	}
	
	/**
	 * 查找不可用的优惠券
	 */
	@Override
	public PageInfo<Coupon> getDisableCoupon(int userId, BigDecimal orderAmt, int goodsId, int goodsType, int pageNum, int pageSize) {
		//创建页面对象
        PageHelper.startPage(pageNum, pageSize);
        

        List<Coupon> couponList = couponDao.selectDisableCoupon(userId, orderAmt, goodsId, goodsType);
		//生成页面信息
        return new PageInfo<Coupon>(couponList);
	}
	
	


	/**
	   *   优惠券发行
	 */
	@Override
	public void issueCoupon(Coupon coupon) {
		couponDao.insertCoupon(coupon);
	}

	/**
	 * 检车并更新100条优惠券过期状态
	 */
	@Override
	public int checkExpireCoupon() {
		return couponDao.updateExpireCoupon();
	}


	

}
